package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.AcctVerifyRequest;
import cc.rengu.igas.channel.cups.facade.response.AcctVerifyResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.HexUtil;

/**
 * 账户验证
 *
 * @author Administrator
 */
public class CupsAcctVerifyService extends BasePayService<AcctVerifyRequest, AcctVerifyResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, AcctVerifyRequest acctVerifyRequest, AcctVerifyResponse acctVerifyResponse) throws Exception {
        CupsField cupsField = cupsFieldBean.getCupsField();
        cupsFieldBean.setCupsField(cupsField);

        BeanUtil.beanCopy(acctVerifyRequest, cupsField);
        cupsField.setMsgType("0100");
        cupsField.setProcCode("330000");
        cupsField.setConCode("00");
        cupsField.setLocalDate(acctVerifyRequest.getTransDateTime().substring(0, 4));
        cupsField.setLocalTime(acctVerifyRequest.getTransDateTime().substring(4));
        /**柜面*/
        // 目前只有ATM转账用到账户验证，48域用法写死07
        if ("01".equals(acctVerifyRequest.getTermType())) {
            cupsField.setPrivAddData(HexUtil.StringtoHex("ASAO00207"));
        } else if ("06".equals(acctVerifyRequest.getTermType())) {
            cupsField.setPrivAddData(HexUtil.StringtoHex("ASAO00215"));
        }

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        cupsFieldBean.createF60()
                .setF60_2_2(acctVerifyRequest.getTermRead())
                .setF60_2_3(acctVerifyRequest.getIcConCode())
                .setF60_2_5(acctVerifyRequest.getTermType());
        cupsFieldBean.build();

        String piSubField = packF104Pi(acctVerifyRequest.getTermInfo());
        if (piSubField != null) {
            cupsField.setTermAttachData(piSubField);
        }

        //  调用 CUPS
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean);
        // 应答赋值
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, acctVerifyResponse);
        acctVerifyResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (CupsParamConstant.SUCCESS_CODE.equals(acctVerifyResponse.getRespCode())) {
            acctVerifyResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            acctVerifyResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(acctVerifyResponse);
    }
}
